package com.tater;

import com.tater.component.ConsumerComponent;
import com.tater.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.*;

public class ComsumerMain {

    // 定义一个线程池
    int corePoolSize = 10;
    int maximumPoolSize =50;
    long keepAliveTime = 100L;
    TimeUnit unit = TimeUnit.SECONDS;
    BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(5000);
    ThreadFactory threadFactory = Executors.defaultThreadFactory();
    RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();


    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            corePoolSize,
            maximumPoolSize,
            keepAliveTime,
            unit,
            workQueue,
            threadFactory,
            handler
    );

    public void startRequest(ConsumerComponent consumerComponent){
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                consumerComponent.methodA("methodA");
                consumerComponent.methodB("methodB");
                consumerComponent.methodC("methodC");
            }
        });
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(DubboConfig.class);
        context.start();

        ComsumerMain main=new ComsumerMain();

        ConsumerComponent consumerComponent = context.getBean(ConsumerComponent.class);

        while (true){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 60; i++) {
                main.startRequest(consumerComponent);
            }

//            String result = consumerComponent.callMethod("tater");


            //System.out.println(result);
        }
    }

    @Configuration
    @ComponentScan("com.tater")
    @EnableDubbo
    @PropertySource("classpath:/dubbo-consumer.properties")
    static class DubboConfig{
    }
}
